package com.dbf.site.client.user.presenter;

import com.dbf.site.client.ForumEventBus;
import com.dbf.site.client.user.model.UserModel;
import com.dbf.site.client.user.service.UserServiceAsync;
import com.dbf.site.client.user.view.IUserProfileView;
import com.dbf.site.client.user.view.UserProfileView;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

@Presenter(view = UserProfileView.class)
public class UserProfilePresenter
    extends BasePresenter<IUserProfileView, ForumEventBus>
    implements IUserProfileView.IUserProfilePresenter {

  private UserModel user;
  private UserModel userCopy = new UserModel();

  @Inject
  private UserServiceAsync service;

  @Override
  public void bind() {
  }
  
  public void onStart() {
//	  eventBus.setProfileView(view.asWidget());
  }


  @Override
  public void doCreateUser(UserModel user) {
    service.createUser(user,
                       new AsyncCallback<UserModel>() {
                         @Override
                         public void onFailure(Throwable caught) {
                           Window.alert("PANIC!");
                         }

                         @Override
                         public void onSuccess(UserModel result) {
                        	 Window.alert("Bienvenu sur notre forum de discussion!");
                         }
                       });
  }

  @Override
  public void doUpdateUser(UserModel user) {
    service.updateUser(user,
                       new AsyncCallback<UserModel>() {
                         @Override
                         public void onFailure(Throwable caught) {
                           Window.alert("PANIC!");
                         }

                         @Override
                         public void onSuccess(UserModel result) {
                        	 Window.alert("Vos données ont été mises à jour avec sccès");
                         }
                       });
  }

  public void onCreateNewUser(UserModel user) {
    this.user = user;
    view.clear();
    view.showUser(this.user, true);
  }


@Override
public void doCancel() {
	// TODO Auto-generated method stub
	
}

}
